package com.poster.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class StringUtils {

    private static final DateTimeFormatter fmt;

    static {
       fmt = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    }


    private StringUtils() {

    }


    public static boolean isEmpty(String str){
        return str == null ||str.isBlank();
    }

    public static boolean pathIsOk(String path){
        return !isEmpty(path) && !path.contains("..");
    }

    public static String formatTime(LocalDateTime time){
        return fmt.format(time);
    }

    public static String md5(String str){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(str.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("不支持md5", e);
        }
    }

}
